package com.yunda.AJDeviceService.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunda.AJDeviceService.dao.DeviceRunTimeDao;
import com.yunda.AJDeviceService.data.DeviceRunStatusData;
import com.yunda.AJDeviceService.data.DeviceRunTimeData;
import com.yunda.AJDeviceService.data.entity.GetRunTimeParam;
import com.yunda.AJDeviceService.service.DeviceRunTimeService;
import com.yunda.AJDeviceService.utils.DateUtils;

/**
 * AJDeviceService
 * @description 安检机运行时间处理
 * @author ljc
 * @create 2023年2月11日 上午10:32:00
 */
@Service
public class DeviceRunTimeServiceImpl extends ServiceImpl<DeviceRunTimeDao, DeviceRunTimeData> implements DeviceRunTimeService{

	//更新本次开机的结束时间
	@Override
	public void updateEndTime(DeviceRunStatusData statusData) {
		QueryWrapper<DeviceRunTimeData> queryWrapper=new QueryWrapper<>();
		queryWrapper.select("id")
		.eq("fb_code", statusData.getFbCode()).eq("device_supplier", statusData.getDeviceSupplier())
		.eq("device_code", statusData.getDeviceCode()).orderByDesc("create_time").last("limit 1");
		Map<String, Object> map=getMap(queryWrapper);
		if(!ObjectUtils.isEmpty(map)) {
			UpdateWrapper<DeviceRunTimeData> updateWrapper=new UpdateWrapper<>();
			updateWrapper.eq("id", map.get("id"));
			updateWrapper.set("end_time", DateUtils.getTime());
			update(updateWrapper);
		}else {
			//写入数据库开始时间
			String nowTime=DateUtils.getTime();
			DeviceRunTimeData timeData=new DeviceRunTimeData();
			timeData.setFbCode(statusData.getFbCode());
			timeData.setDeviceSupplier(statusData.getDeviceSupplier());
			timeData.setDeviceCode(statusData.getDeviceCode());
			timeData.setStartTime(nowTime);
			timeData.setEndTime(nowTime);
			timeData.setCreateTime(nowTime);
			save(timeData);
		}
	}
	
	/**
	 * 获取设备运行时长
	 */
	@Override
	public double getRunTime(GetRunTimeParam param) {
		QueryWrapper<DeviceRunTimeData> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("fb_code", param.getFb_code()).eq("device_supplier", param.getDevice_supplier())
		.eq("device_code", param.getDevice_code()).lt("start_time", param.getEnd_time()).gt("end_time", param.getStart_time());
		List<DeviceRunTimeData> timeList= this.list(queryWrapper);
		if(ObjectUtils.isEmpty(timeList)) {
			return 0;
		}else {
			long datePoor=0;
			for(DeviceRunTimeData timeData:timeList) {
				if(!DateUtils.dateTime(timeData.getStartTime()).equals(DateUtils.dateTime(timeData.getEndTime()))) {//跨天工作
					if(DateUtils.dateTime(timeData.getStartTime()).equals(DateUtils.dateTime(param.getStart_time()))) {
						timeData.setEndTime(param.getEnd_time());
					}else {
						timeData.setStartTime(param.getStart_time());
					}
				}
				datePoor+=DateUtils.getDiffTime(DateUtils.parseDate(timeData.getStartTime()),
						DateUtils.parseDate(timeData.getEndTime()));
			}
	        BigDecimal b = new BigDecimal((double)datePoor/60);
	        Double hour = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();//第一个参数是保留小数的位数
			return hour;
		}
	}
	
}
